package com.htime.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Created by HTime on 2017/3/21.
 */
@RestController
public class SessionByCookieController  extends HttpServlet {

    @RequestMapping(value = "/sessionInCookie", method = RequestMethod.GET)
    public String sessionInCookie(HttpServletRequest req, HttpServletResponse resp) {

        //获取session
        HttpSession session = req.getSession();

        if (session.isNew()) {
            // 设置session属性值
            session.setAttribute("SessionName", "Htime");
        }
        // 获取SessionId
        String sessionId = session.getId();

        if (session.isNew()) {
            return "第一次请求,sessionId = " + sessionId;
        } else {
            return "第二次请求,sessionId = " + sessionId + " sessionName = " + session.getAttribute("SessionName");
        }

    }

}
